package com.ecy.quadratic.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ecy.quadratic.R;
import com.ecy.quadratic.reader.utils.ScreenUtils;
import com.yuyh.easyadapter.glide.GlideCompressTransform;

import java.util.ArrayList;

public class CommunityGridViewAdapter extends BaseAdapter {

    Context context;
    ArrayList<String> list = new ArrayList<>();
    private int width;
    private int height;

    public CommunityGridViewAdapter(Context context) {
        this.context = context;
        this.width = (ScreenUtils.getScreenWidth() - ScreenUtils.dpToPxInt(36)) / 3;
    }

    public void updateData(ArrayList<String> data, int w, int h) {
        width = w;
        height = h;
        this.list.clear();
        this.list.addAll(data);
        notifyDataSetChanged();
    }

    public void clear() {
        this.list.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }


    @Override
    public View getView(final int position, View view, ViewGroup arg2) {
        Holder holder;
//		if (view==null) {
        view = LayoutInflater.from(context).inflate(R.layout.item_community_gridview, null);
//			holder=new Holder();
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
//			view.setTag(holder);
//		}else {
//			holder= (Holder) view.getTag();
//		}
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        layoutParams.width = width;
        layoutParams.height = height;
//		new AbsListView.LayoutParams(wh,wh);
        imageView.setLayoutParams(layoutParams);
//		imageView.setImageMatrix(new Matrix());
        Glide.with(context).load(Uri.parse(list.get(position)))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .transform(new GlideCompressTransform(context, width))
                .placeholder(R.drawable.cover_default_broad)
                .dontAnimate()
                .into(imageView);
        return view;
    }

    class Holder {
        ImageView imageView;
    }


}
